//
//  MineSetController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/7/6.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "MineSetController.h"

@interface MineSetController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *yuanPassField;
@property (weak, nonatomic) IBOutlet UITextField *nePassField;
@property (weak, nonatomic) IBOutlet UITextField *reField;

@end

@implementation MineSetController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
  self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonTitle:@"保存" image:nil frame:CGRectMake(0, 0, 30, 20) target:self sel:@selector(sureClick)];
    
}
-(void)setUI
{
    self.yuanPassField.tag = 102;
    self.nePassField.tag = 103;
    self.reField.tag = 104;
    self.yuanPassField.delegate = self;
    self.nePassField.delegate = self;
    self.reField.delegate = self;
    [self.yuanPassField setSecureTextEntry:NO];
    [self.nePassField setSecureTextEntry:NO];
    [self.reField setSecureTextEntry:NO];
}

#pragma mark UITextFiledDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    return YES;
}

-(void)sureClick
{
    if(self.yuanPassField.text.length==0){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"愿密码不能为空"
                                                                       message:nil
                                                                preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定"
                                                         style:UIAlertActionStyleDefault
                                                       handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }else if (self.nePassField.text.length==0){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"新密码不能为空"
                                                                       message:nil
                                                                preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定"
                                                         style:UIAlertActionStyleDefault
                                                       handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }else if (![self.reField.text isEqualToString:self.nePassField.text]){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"两次输入的密码不相同"
                                                                       message:nil
                                                                preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* action = [UIAlertAction actionWithTitle:@"确定"
                                                         style:UIAlertActionStyleDefault
                                                       handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        [self requestXin];
    }

}

-(void)requestXin
{
    NSDictionary *params = @{@"accountname":[us objectForKey:kUserid],
                             @"newpass":self.nePassField.text,
                             @"oldpass":self.yuanPassField.text,
                             };
    [NetWorkManager requestWithType:1 withUrlString:@"/informationsite/updatepassword.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"status"] integerValue]==1) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
        
    } progress:^(float progress) {
        
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
